package com.run_java_test.t.test_code;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.net.URL;
import java.net.URLClassLoader;

@Configuration
public class ControllerLoader {

    @Autowired
    private ApplicationContext context;
    @Autowired
    private ControllerMappingChecker controllerMappingChecker;

    @Bean
    public A testA() {
        controllerMappingChecker.checkControllerMappings();
        return new A();
    }


    // 动态加载控制器类并注册为 Bean
    public void loadController(String classPath) throws Exception {
        // 创建一个新的类加载器，加载目标路径下的类文件
        URL[] urls = {new URL("file:" + classPath)};  // 目标文件夹路径
        URLClassLoader loader = new URLClassLoader(urls, this.getClass().getClassLoader());

        // 加载指定的控制器类
        Class<?> controllerClass = loader.loadClass("com.run_java_test.test_code.DynamicController");

        // 动态注册该控制器 Bean
        if (context instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context;
            GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
            beanDefinition.setBeanClass(controllerClass);
            registry.registerBeanDefinition("dynamicController", beanDefinition);
        }
    }

    // 手动销毁动态加载的 Bean
    public void destroyController(String beanName) {
        if (context instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context;
            registry.removeBeanDefinition(beanName);
        }
    }

    // 动态注册控制器 Bean
    public static void registerController(Class<?> controllerClass, GenericWebApplicationContext context, RequestMappingHandlerMapping requestMappingHandlerMapping) {
//        if (controllerClass.isAnnotationPresent(RestController.class)) {
//            GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
//            beanDefinition.setBeanClass(controllerClass);
//            context.registerBeanDefinition(controllerClass.getSimpleName(), beanDefinition);
//        }

        if (controllerClass.isAnnotationPresent(RestController.class)) {
            context.registerBean(controllerClass);
            // 确保映射到 RequestMappingHandlerMapping
            // requestMappingHandlerMapping.afterPropertiesSet();
        }
    }

    // 卸载控制器 Bean
    public static void unloadController(String beanName, ApplicationContext context) {
        if (context instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context;

            // 移除 Bean 定义
            if (registry.containsBeanDefinition(beanName)) {
                registry.removeBeanDefinition(beanName);
                System.out.println("Bean " + beanName + " has been removed.");
            } else {
                System.out.println("Bean " + beanName + " not found.");
            }
        }
    }
}
